#include "order/AnalyExcelCompare.h"
#define   _IntergerMap_ _RdoMap_Integer_
ExcelCompare::ExcelCompare(QMap<QString, QList<QString>> &RdoMapListChar, QMap<QString, QList<QList<QString>>> &RdoMapListNestChar, bool caseSensitive)
            : _caseSensitive(caseSensitive){
    RdoMap_ListChar = &RdoMapListChar;
    RdoMap_ListNestChar = &RdoMapListNestChar;
    definfc.ColExtendToMapInt(colMap, 2);

}

void ExcelCompare::run(){
//    CPR_Mutex.lock();
//    OleInitialize(0);
//    QAxObject *Nobject = new QAxObject("Excel.Application");
//    OleUninitialize();
//    Nobject->dynamicCall("SetVisible(bool)", false);
//    QAxObject *WorkExcels = Nobject->querySubObject("WorkBooks");
//    int indexOf = 0;
//    QMap<QString, QMap<QString, QMap<QString, QStringList>>> sourceMap;
//    qDebug() << (*RdoMap_ListChar)["ExcelCompare-SourceFile"] << (*RdoMap_ListNestChar)["ExcelCompare-sourceSheet"] << (*RdoMap_ListNestChar)["ExcelCompare-sourceIndex"];
//    foreach(QString file, (*RdoMap_ListChar)["ExcelCompare-SourceFile"])
//    {
////        open(WorkExcels, file, (*RdoMap_ListNestChar)["ExcelCompare-sourceSheet"][indexOf], (*RdoMap_ListNestChar)["ExcelCompare-sourceIndex"][indexOf], sourceMap);
//        qDebug() << sourceMap;
//        indexOf++;
//    }
//    QAxObject *
//    QAxObject *
//    QAxObject *
//    QAxObject *
//    QAxObject *

//    CPR_Mutex.unlock();
}


void ExcelCompare::open(QAxObject *WorkExcels, QString _file, QStringList _sheet, QStringList _col, QMap<QString, QMap<QString, QMap<QString, QStringList>>> &get){
    QAxObject *WorkExcel = WorkExcels->querySubObject("Open(const QString&)", _file);
    QAxObject *ExcelSheets = WorkExcel->querySubObject("WorkSheets");
    QAxObject *ExcelSheetChild;
    foreach (QString var, _sheet)
    {
//        if (var.isEmpty())
//        {
//            ExcelSheetChild = ExcelSheets->querySubObject("ActiveSheet");
//        }
//        else
//        {
//            if (isInt(var))
//            {
//                ExcelSheetChild = ExcelSheets->querySubObject("sheet(int)", var.toUInt());
//            }
//            else
//            {
//                ExcelSheetChild = ExcelSheets->querySubObject("sheet(QString)", var);
//            }
//        }
//        qDebug() << isInt(var) << var;
//        QAxObject *ExcelSheetRange;
//        foreach (QString col, _col)
//        {
//            if (isInt(col))
//            {
//                ExcelSheetRange = ExcelSheetChild->querySubObject("Range(QString)", colMap[col.toInt()].c_str());
//            }
//            else
//            {
//                ExcelSheetRange = ExcelSheetChild->querySubObject("Range(QString)", col);
//            }
//            qDebug() << isInt(col) << col;
//            QList<QString> g;
//            QVariant i = ExcelSheetRange->dynamicCall("Value");
//            foreach (QVariant variant, i.toList()) {
//                g.append(variant.toString());
//            }
//            get[_file][var][col] = g;
//        }
    }
}


QMap<QString, QString> ExcelCompare::compareExcelMap(QMap<QString, QString> &_T, QMap<QString, QString> &_P){
    QStringList keysT = _T.keys();
    QStringList keysP = _P.keys();
    QStringList valuesT = _T.values();
    QStringList valuesP = _P.values();
    QMap<QString, QString> result;
    uint sz;
    bool how = (_T.size() > _P.size());
    how ? sz = _T.size() : sz = _P.size();
    for(uint i = 0; i < sz; i++)
    {
        if (how)
        {
            QString t = valuesT[i];
            if (!valuesP.contains(t))
            {
                result.insert(keysT[i], valuesT[i]);
            }
            if (i + 1 <= (uint)_P.size())
            {
                QString p = valuesP[i];
                if (!valuesT.contains(p))
                {
                    result.insert(keysP[i], valuesP[i]);
                }
            }
        }
        else
        {
            QString p = valuesP[i];
            if (!valuesT.contains(p))
            {
                result.insert(keysP[i], valuesP[i]);
            }
            if (i + 1 <= (uint)_T.size())
            {
                QString t = valuesT[i];
                if (!valuesP.contains(t))
                {
                    result.insert(keysT[i], valuesT[i]);
                }
            }
        }
    }
    return result;
}
